"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const geoserver_js_1 = require("./util/geoserver.js");
class SettingsClient {
    constructor(url, auth) {
        this.url = url;
        this.auth = auth;
    }
    async getSettings() {
        const response = await fetch(this.url + 'settings.json', {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return response.json();
    }
    async updateSettings(settings) {
        const response = await fetch(this.url + 'settings', {
            credentials: 'include',
            method: 'PUT',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(settings)
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
    }
    async updateProxyBaseUrl(proxyBaseUrl) {
        const settingsJson = await this.getSettings();
        if (!settingsJson.global && !settingsJson.global.settings) {
            return false;
        }
        settingsJson.global.settings.proxyBaseUrl = proxyBaseUrl;
        await this.updateSettings(settingsJson);
    }
    async getContactInformation() {
        const response = await fetch(this.url + 'settings/contact', {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return response.json();
    }
    async updateContactInformation(address, city, country, postalCode, state, email, organization, contactPerson, phoneNumber) {
        const contact = {
            address: address,
            addressCity: city,
            addressCountry: country,
            addressPostalCode: postalCode,
            addressState: state,
            contactEmail: email,
            contactOrganization: organization,
            contactPerson: contactPerson,
            contactVoice: phoneNumber
        };
        const body = {
            contact: contact
        };
        const url = this.url + 'settings/contact';
        const response = await fetch(url, {
            credentials: 'include',
            method: 'PUT',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(body)
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
    }
}
exports.default = SettingsClient;
